﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;

/// <summary>
/// Summary description for AppUtils
/// </summary>
public class AppUtils
{
    public AppUtils()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public static int Request(string name)
    {
        try
        {
            return Convert.ToInt32(HttpContext.Current.Request[name]);
        }
        catch
        {
            return 0;
        }
    }

    public static string ImageUrl(string url, string w, string h)
    {
        return ConfigurationManager.AppSettings["MEDIA"] + url + "." + w + "." + h + ".image";
    }

    public static string ImageUrlOriginal(string url)
    {
        return ConfigurationManager.AppSettings["MEDIA.PATH"] + url;
    }

    public static string FilterTextbox(string s)
    {
        return s.Replace("'", "*")
            .Replace("<", "*")
            .Replace(">", "*")
            .Replace("js", "*")
            ;
    }
    public static string SubTitle(string input, int charLenght)
    {
        if (input.Length > charLenght)
        {
            while (charLenght > 0 && input[charLenght] != ' ')
            {
                charLenght--;
            }
            return input.Substring(0, charLenght) + "...";
        }
        else
        {
            return input;
        }
    }

    public static string CateReWriteUrl(int cateid, string catename)
    {
        string urlrewrite = "";
        urlrewrite = "/" + UCS2Convert(catename) + "/" + cateid + "-0/index.html";
        return urlrewrite;
    }

    public static string DayOfWeekVN(DayOfWeek dow)
    {
        string _return = string.Empty;
        switch (dow)
        {
            case DayOfWeek.Monday:
                _return = "Thứ Hai";
                break;
            case DayOfWeek.Tuesday:
                _return = "Thứ Ba";
                break;
            case DayOfWeek.Wednesday:
                _return = "Thứ Tư";
                break;
            case DayOfWeek.Thursday:
                _return = "Thứ Năm";
                break;
            case DayOfWeek.Friday:
                _return = "Thứ Sáu";
                break;
            case DayOfWeek.Saturday:
                _return = "Thứ Bảy";
                break;
            case DayOfWeek.Sunday:
                _return = "Chủ Nhật";
                break;
        }

        return _return;
    }
    public static String UCS2Convert(String sContent)
    {
        sContent = sContent.Trim();
        String sUTF8Lower = "a|á|à|ả|ã|ạ|ă|ắ|ằ|ẳ|ẵ|ặ|â|ấ|ầ|ẩ|ẫ|ậ|đ|e|é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ|i|í|ì|ỉ|ĩ|ị|o|ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ|u|ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự|y|ý|ỳ|ỷ|ỹ|ỵ";

        String sUTF8Upper = "A|Á|À|Ả|Ã|Ạ|Ă|Ắ|Ằ|Ẳ|Ẵ|Ặ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ|Đ|E|É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ|I|Í|Ì|Ỉ|Ĩ|Ị|O|Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ|U|Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự|Y|Ý|Ỳ|Ỷ|Ỹ|Ỵ";

        String sUCS2Lower = "a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|d|e|e|e|e|e|e|e|e|e|e|e|e|i|i|i|i|i|i|o|o|o|o|o|o|o|o|o|o|o|o|o|o|o|o|o|o|u|u|u|u|u|u|u|u|u|u|u|u|y|y|y|y|y|y";

        String sUCS2Upper = "A|A|A|A|A|A|A|A|A|A|A|A|A|A|A|A|A|A|D|E|E|E|E|E|E|E|E|E|E|E|E|I|I|I|I|I|I|O|O|O|O|O|O|O|O|O|O|O|O|O|O|O|O|O|O|U|U|U|U|U|U|U|U|U|U|U|U|Y|Y|Y|Y|Y|Y";

        String[] aUTF8Lower = sUTF8Lower.Split(new Char[] { '|' });

        String[] aUTF8Upper = sUTF8Upper.Split(new Char[] { '|' });

        String[] aUCS2Lower = sUCS2Lower.Split(new Char[] { '|' });

        String[] aUCS2Upper = sUCS2Upper.Split(new Char[] { '|' });

        Int32 nLimitChar;

        nLimitChar = aUTF8Lower.GetUpperBound(0);

        for (int i = 1; i <= nLimitChar; i++)
        {

            sContent = sContent.Replace(aUTF8Lower[i], aUCS2Lower[i]);

            sContent = sContent.Replace(aUTF8Upper[i], aUCS2Upper[i]);

        }
        string sUCS2regex = @"[A-Za-z0-9- ]";
        string sEscaped = new Regex(sUCS2regex, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.ExplicitCapture).Replace(sContent, string.Empty).ToLower();
        if (string.IsNullOrEmpty(sEscaped))
            return sContent.Replace(" ","-").ToLower();
        sEscaped = sEscaped.Replace("[", "\\[");
        sEscaped = sEscaped.Replace("]", "\\]");
        sEscaped = sEscaped.Replace("^", "\\^");
        string sEscapedregex = @"[" + sEscaped + "]";
        return new Regex(sEscapedregex, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.ExplicitCapture).Replace(sContent, string.Empty).ToLower();
    }
}
